
package socket;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 */
public class Client {

    private Socket socket;
    private JTextArea chatArea;
    private JTextField inputField;
    private JFrame frame;
    private PrintWriter toServer;

    public Client() {
        try {
            System.out.println("正在连接服务端...");
            socket = new Socket("localhost", 8088);
            System.out.println("服务端连接成功!");

            // 初始化GUI
            initializeGUI();

            // 获取输出流
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            toServer = new PrintWriter(bw, true);

            // 启动用于读取服务端消息的线程
            ServerHandler handler = new ServerHandler();
            Thread t = new Thread(handler);
            t.setDaemon(true);
            t.start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initializeGUI() {
        // 创建JFrame
        frame = new JFrame("Chat Client");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        // 文本区域
        chatArea = new JTextArea(20, 40);
        chatArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(chatArea);

        // 输入框
        inputField = new JTextField(20);

        // 发送按钮
        JButton sendButton = new JButton("Send");
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = inputField.getText();
                if (!text.trim().isEmpty()) {
                    toServer.println(text);
                    inputField.setText("");
                }
            }
        });

        // 布局管理器
        BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
        frame.setLayout(layout);

        // 添加组件
        frame.add(scrollPane);
        JPanel inputPanel = new JPanel();
        inputPanel.add(inputField);
        inputPanel.add(sendButton);
        frame.add(inputPanel);

        // 显示窗口
        frame.setVisible(true);
    }

    public void start() {
        Scanner scan = new Scanner(System.in);
        while (true) {
            String nickname = scan.nextLine();
            if (nickname.trim().isEmpty()) {
                System.out.println("用户名不能为空");
            } else {
                toServer.println(nickname);
                System.out.println("欢迎你:" + nickname + ",请开始聊天吧");
                break;
            }
        }

        // 主线程不需要再处理任何逻辑
    }

    private class ServerHandler implements Runnable {
        public void run() {
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                String message;
                while ((message = br.readLine()) != null) {
                    chatArea.append(message + "\n");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
    }
}